package com.funny.stock.base.util;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl;

import java.beans.PropertyDescriptor;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

public class PropertiesCopyUtil {

    /**
     * 属性复制
     * 
     * @param source
     * @param clazz
     * @return
     */
    public static <T> T copyProperties(Object source, Class<T> clazz) {
        if (source == null) {
            return null;
        }
        try {
            T target = BeanUtils.instantiate(clazz);
            BeanUtils.copyProperties(source, target);
            return target;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void copyProperties(Object source, Object target) {
//        if (source == null) {
//            return null;
//        }
        try {
            BeanUtils.copyProperties(source, target);
//            return target;
        } catch (Exception e) {
            e.printStackTrace();
        }
//        return null;
    }

    public static <T> List<T> copyPropertiesByList(List sourceList, Class<T> clazz) {
        if (sourceList == null) {
            return null;
        }

        List<T> list = new ArrayList<>();

        for (int i = 0; i < sourceList.size(); i++) {
            T item = copyProperties(sourceList.get(i), clazz);
            list.add(item);
        }

        return list;
    }


    /**
     * 忽略部分属性
     * 
     * @param source
     * @param clazz
     * @param ignoreProperties
     * @return
     */
    public static <T> T copyPropertiesIgnore(Object source, Class<T> clazz,
                                             String... ignoreProperties) {
        if (source == null) {
            return null;
        }
        try {
            T target = BeanUtils.instantiate(clazz);
            BeanUtils.copyProperties(source, target, ignoreProperties);
            return target;
        } catch (Exception e) {
        }
        return null;
    }


    /**
     * 复制部分属性
     * 
     * @param source
     * @param clazz
     * @param specificProperties
     * @return
     */
    public static <T> T copyPropertiesSpecific(Object source, Class<T> clazz,
                                               String... specificProperties) {
        if (source == null) {
            return null;
        }
        try {
            T target = BeanUtils.instantiate(clazz);
            if (specificProperties == null) {
                return target;
            }
            List<String> specificList = Arrays.asList(specificProperties);
            copySpecificProperties(source, target, specificList);
            return target;
        } catch (Exception e) {
        }
        return null;
    }


    private static void copySpecificProperties(final Object source,
                                               final Object target, final Iterable<String> properties) {


        final BeanWrapper src = new BeanWrapperImpl(source);
        final BeanWrapper trg = new BeanWrapperImpl(target);


        for (final String propertyName : properties) {
            trg.setPropertyValue(propertyName, src.getPropertyValue(propertyName));
        }

    }

    public static void modifyCreateIdProps(final Object source,int createId){
        final BeanWrapper src = new BeanWrapperImpl(source);

        try {
            PropertyDescriptor createIdDesc = src.getPropertyDescriptor("createId");

//            String date2String = DateUtil.date2String(new Date().getTime(), "yyyy-MM-dd HH:mm:ss");
            src.setPropertyValue("createId",createId);
            src.setPropertyValue("createTime",new Date());
        }catch (Exception e){

        }
    }

    public static void modifyUpdateIdProps(final Object source,int updateId){
        final BeanWrapper src = new BeanWrapperImpl(source);

        try {
            PropertyDescriptor createIdDesc = src.getPropertyDescriptor("updateId");

//            String date2String = DateUtil.date2String(new Date().getTime(), "yyyy-MM-dd HH:mm:ss");
            src.setPropertyValue("updateId",updateId);
            src.setPropertyValue("updateTime",new Date());
        }catch (Exception e){

        }
    }



}
